home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Java / Magic Oracle / BufferedImage.java < prev    next >
Encoding:
Java Source  |  2000-09-28  |  2.9 KB  |  129 lines  |  [TEXT/SNJ2]

  1. /**
  2.  *  Apple Worldwide Developer Technical Support
  3.  *
  4.  *  Sample demonstrating offscreen drawing in Java.
  5.  *
  6.  *  by Levi Brown, Apple Developer Technical Support
  7.  *
  8.  *  File:   BufferedImage.java
  9.  *
  10.  *  Copyright ©1999 Apple Computer, Inc.
  11.  *  All rights reserved.
  12.  *
  13.  *    4/99    v. 1.0    Shipped as 'Magic Oracle AppleScript for Java' sample.
  14.  *
  15.  *  You may incorporate this sample code into your applications without
  16.  *  restriction, though the sample code has been provided "AS IS" and the
  17.  *  responsibility for its operation is 100% yours.  However, what you are
  18.  *  not permitted to do is to redistribute the source as "Apple Sample
  19.  *  Code" after having made changes. If you're going to re-distribute the
  20.  *  source, we require that you make it clear in the source that the code
  21.  *  was descended from Apple Sample Code, but that you've made changes.
  22. **/
  23.  
  24. import java.awt.*;
  25.  
  26. /**
  27.  * @author Levi Brown
  28.  * @author Apple Worldwide Developer Technical Support
  29.  * @author Apple Computer Inc.
  30.  */
  31. public class BufferedImage extends BufferedDrawer
  32. {
  33.     public BufferedImage()
  34.     {
  35.         image = null;
  36.         imageLoc = new Point(0, 0);
  37.         imageSize = new Dimension(0, 0);
  38.     }
  39.     
  40.     public void setImage(Image image)
  41.     {
  42.         if (image != null)
  43.         {
  44.             this.image = image;
  45.             imageSize.width = image.getWidth(this);
  46.             imageSize.height = image.getHeight(this);
  47.         }
  48.     }
  49.  
  50.     public Image getImage()
  51.     {
  52.         return image;
  53.     }
  54.     
  55.     public void setImageLocation(Point loc)
  56.     {
  57.         if (!imageLoc.equals(loc))
  58.         {
  59.             imageLoc = loc;
  60.             draw();
  61.         }
  62.     }
  63.     
  64.     public Point getImageLocation()
  65.     {
  66.         return imageLoc;
  67.     }
  68.  
  69.     public Dimension getImageSize()
  70.     {
  71.         return imageSize;
  72.     }
  73.     
  74.     /** 
  75.      * Returns the preferred size of this component.
  76.      * @see #getMinimumSize
  77.      * @see LayoutManager
  78.      */
  79.     public Dimension getPreferredSize()
  80.     {
  81.         if (image == null)
  82.         {
  83.             return super.getPreferredSize();
  84.         }
  85.         else
  86.         {
  87.             return getImageSize();
  88.         }
  89.     }
  90.  
  91.     /**
  92.      * Handles drawing the desired content into the offscreen buffer.
  93.      * Override this to do your own drawing (be sure to call super.draw()).
  94.      * @see #paint
  95.      */
  96.     protected void draw()
  97.     {
  98.         super.draw();
  99.         if (image != null)
  100.         {
  101.             Dimension s = getSize();
  102.             workingGraphics.setColor(getBackground());
  103.             workingGraphics.fillRect(0, 0, s.width, s.height);
  104.             workingGraphics.drawImage(image, imageLoc.x, imageLoc.y, this);
  105.         }
  106.     }
  107.     
  108.     protected Image image;
  109.     protected Point imageLoc;
  110.     protected Dimension imageSize;
  111.  
  112.     /**
  113.      * Notifies the Component that it has been added to a container
  114.      * and if a peer is required, it should be created.
  115.      * This method should be called by Container.add, and not by user
  116.      * code directly.
  117.      * @see #removeNotify
  118.      */
  119.     public void addNotify()
  120.     {
  121.         super.addNotify();
  122.         if (image != null)
  123.         {
  124.             Dimension size = getSize();
  125.             setImageLocation(new Point((size.width - imageSize.width) >> 1, (size.height - imageSize.height) >> 1));
  126.         }
  127.     }
  128. }
  129.